"use strict";

var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");

var _includes = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/includes"));

/**
 * 解析URL的参数部分
 *
 * @since 0.0.4
 * @category 解析
 * @param {string|number} str 需要解析的url.
 * @param {object} config 配置参数.
 * @param {boolean} config.isDetail 是否返回详细信息, 为true时返回CheckResult, false时返回返回解析的query对象 默认: false.
 *
 * @returns { CheckResult } 结果对象.
 *
 * @typedef {object} CheckResult
 * @property {boolean} status 解析是否成功
 * @property {string} errorMsg 错误提示
 * @property {object} result 解析出的query对象
 * @example
 *
 * isEmail('https://connect.zto.com/oauth2/authorize?appid=ztIbe8d26TX36joEQo6S9b6w&redirect_uri=https%3A%2F%2Fportal.gw.zt-express.com%2FloginCallback%3Fns%3Dportal%26redirect%3Dhttps%253A%252F%252Fe.zto.com%252Fe%252FztMoqCB00IX56QAeCYv_oxBg%252Fcustomer%252Faccount%252Findex&response_type=code&scope=userinfo,user_id,usercontact,userprofile&state=12')
 * // { status: true, errorMsg: '', result: {"appid":"ztIbe8d26TX36joEQo6S9b6w","redirect_uri":"https%3A%2F%2Fportal.gw.zt-express.com%2FloginCallback%3Fns%3Dportal%26redirect%3Dhttps%253A%252F%252Fe.zto.com%252Fe%252FztMoqCB00IX56QAeCYv_oxBg%252Fcustomer%252Faccount%252Findex","response_type":"code","scope":"userinfo,user_id,usercontact,userprofile","state":"12"}}
 *
 */
function parseUrl(url) {
  var _ref = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {},
      _ref$isDetail = _ref.isDetail,
      isDetail = _ref$isDetail === void 0 ? false : _ref$isDetail;

  var rt = {
    status: false,
    errorMsg: '',
    result: null
  }; // 没有传入url

  if (!url) {
    rt.errorMsg = '没有需要解析的url';
  } else if (!(0, _includes.default)(url).call(url, '?')) {
    // 判断是否有query部分
    rt.errorMsg = '没有需要解析的参数部分';
  } else {
    // 获取query部分
    var queryStr = url.split('?')[1]; // 解析queryStr部分

    var queryArr = queryStr.split('&'); // 生成query对象体

    var queryObj = {};
    queryArr.forEach(function (item) {
      var key = item.split('=')[0];
      var value = item.split('=')[1];
      queryObj[key] = value;
    });

    if (queryArr.length) {
      rt.status = true;
      rt.result = queryObj;
    }
  }

  return isDetail ? rt : rt.result;
}

module.exports = parseUrl;